<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 * 后台顶层类
 * @name    TheAdmin
 * @see
 * @version	1.0.0 (Tue Nov 04 15:54:48 CST 2008)
 * @author	sanshi
 */

//加载配置文件
require_once(dirname(dirname(__FILE__))."/common/config.inc.php");
header("content-type:text/html; charset=utf-8");

class TheAdmin
{
	public $smarty;
	private $menus_file_name ;
	private $permission_file_name;
	private $auto_load = true;
	private $permission_arr = array();
	public $userRequest;
	public  $admin_user_id;
   /**
	* 构造函数
	* @return void
	* @access public
	*/
	public function __construct()
	{
		$this->TheAdmin();
	}
	function TheAdmin()
	{
		$this->userRequest = Factory::getBaseClass('UserRequest');
		//设置加载菜单的文件
		$this->menus_file_name = CACHE_PATH.'admin/menus.data';
		$this->permission_file_name = CACHE_PATH.'admin/permission.data';

		//如果文件过期，则不启动自动加载
		$overTime = 0;//过期时间
		if(time()-fileatime($this->permission_file_name)>$overTime) $this->auto_load = true;

		$this->smarty = Factory::getSmarty();
		$this->smarty->template_dir = TPL_PATH."admin/";
		$this->smarty->compile_dir = COM_PATH."admin/";
		$this->smarty->assign('css_path',CSS_PATH );
		$this->smarty->assign('js_path', JS_PATH);
		$this->smarty->assign('pic_path', PIC_PATH);
		#$this->smarty->assign('upload_max',VIDEO_MAX_SIZE);
		#$this->smarty->assign('up_load_path',HOST_UPLOAD_PATH);
		#$this->smarty->assign('up_transn_path',TRANSN_UPLOAD);
		$this->smarty->assign('main_menus',$this->get_main_menus());
		$this->smarty->register_function('create_popedom',"smarty_create_popedom");
	}
	function get_main_menus()
	{
		$user_popedom = $this->get_user_popedom();
		$menus_arr = $this->get_menus();
		$temp = array();
		for ($i=0;$i<count($menus_arr);$i++)
		{
			if($this->get_user_popedom_by_class_name($menus_arr[$i]['url']))
			{
				$temp[]= "<a href=?c=".$menus_arr[$i]['url'].">".$menus_arr[$i]['title']."</a>";
			}
		}
		if(!IS_ROOT)
		{
			$temp[] = "<a href=".HOST_PATH."login.php?act=login_out>退出</a>";
			$temp[] = "<a href=".ADMIN_HOST_PATH.">返回首页</a>";
		}
		return $temp;
	}
	function check_login()
	{
		$admin_user_id = $this->userRequest->get('admin_user_id');
		if(empty($admin_user_id) && !IS_ROOT) alertMsg(-2002,HOST_PATH.'admin/login.php');
		return $admin_user_id;
	}
	function get_user_popedom()
	{
		return unserialize($this->userRequest->get('admin_user_permission'));
	}
	function get_user_menus()
	{
		//取得用户权限
		$user_popedom = $this->get_user_popedom();
		$menus_arr = $this->get_menus();
		$temp = array();
		for ($i=0;$i<count($menus_arr);$i++)
		{
			if($this->get_user_popedom_by_class_name($menus_arr[$i]['url'],$user_popedom))
				$temp[] = array('url'=>"?c={$menus_arr[$i]['url']}",'title'=>$menus_arr[$i]['title']);
		}
		if(!IS_ROOT) $temp[]=array('url'=>HOST_PATH."admin/login.php?act=login_out",'title'=>"退出");
		return $temp;
	}
	function make_menus()
	{
		//过滤不进入菜单的类名
		$filter_class = array("index","ctrl_prpedom");
		$temp = array();
		$io = Factory::getBaseClass('FileIO');
		$dir_name = ADMIN_PATH.'include/';
		$dir_file_arr = $io->getDirFile($dir_name);
		$permission_arr = array();
		foreach ($dir_file_arr['file'] as $k=>$v)
		{
			$name_arr = explode('.',$v);
			include_once($dir_name.$v);
			$object_name = $name_arr[0];
			if(in_array($object_name,$filter_class)) continue;
			$new_object = new $object_name();
			$class_arr = $new_object->show_p();

			$temp[] = array('url'=>$class_arr['class_name'][0],'title'=>$class_arr['class_name'][1]);

			$permission_arr[$object_name]['title'] = $class_arr['class_name'][1];

			foreach ($class_arr['class_method'] as $k=>$v)
			{
				if($v[2]!=0)
					$permission_arr[$object_name]['method'][] = array($v[1],$v[2]);
			}

			unset($new_object);
			unset($class_arr);
		}
		$io->writeFile($this->menus_file_name,serialize($temp));
		$io->writeFile($this->permission_file_name,serialize($permission_arr));
		return  array($temp,$permission_arr);
	}
	function get_permission_arr()
	{
		if(is_file($this->permission_file_name) && !$this->auto_load)
		{
			$io = Factory::getBaseClass('FileIO');
			return unserialize($io->getFileContent($this->permission_file_name));
		}else {
			$temp = $this->make_menus();
			return $temp[1];
		}
	}
	function get_menus()
	{
		if(is_file($this->menus_file_name) && !$this->auto_load)
		{
			$io = Factory::getBaseClass('FileIO');
			return unserialize($io->getFileContent($this->menus_file_name));
		}else {
			$temp = $this->make_menus();
			return $temp[0];
		}
	}
	function get_user_popedom_by_class_name($class_name)
	{
		$user_popedom_arr = $this->get_user_popedom();
		if(IS_ROOT) return $class_name;
		//判断用户有无权限操作这个类
		return isset($user_popedom_arr[$class_name]) ? $user_popedom_arr[$class_name] : 0;
	}
	//后台权限控制层，使用类
	/**
 	* 检测用户类
 	* @name   user_popedom
 	* @param  string	$class_name 	需要执行的类名
 	* @param  string	$action 	    需要执行的方法名
 	* @return bool 	$return  有权限执行，返回true
 	* @access public
 	* @see
 	* @version	1.0.0 (Tue Nov 04 09:04:48 CST 2008)
 	* @author	sanshi
 	*/
	function user_popedom($class_name,$action)
	{
		$return = false;
		$user_p = $this->get_user_popedom_by_class_name($class_name);
		//实例将要进行操作的类
		$new_class = Factory::getAdminClass($class_name);
		//取得该类的方法权限数组
		$class_p_arr = $new_class->show_p();
		if($class_name=="index")
		{
			$new_class->set();
			$new_class->$action();
			return true;
		}
		//判断要执行的方法是否在类中
		if(isset($class_p_arr['class_method'][$action][2]))
		{
			//取得该方法的权值
			$class_p = $class_p_arr['class_method'][$action][2];
			if(parse_power($user_p,$class_p) || IS_ROOT)
			{
				$new_class->set();
			    $new_class->$action();
			    $return = true;
			}else{
				if($action==DEFANT_METHOD)
				{
					$new_class->set();
					$new_class->_display();
					$return = true;
				}
			}
		}
		echo time() .$class_name."->".$action .($return ? "--有权限" : '--无权限')."</br>";
		unset($new_class);
		//return true;
		return $return;
	}

}
function admin_log($admin_u_id,$action,$log_info,$log_type)
{
	$temp['admin_log_id'] = "";
	$temp['admin_user_id'] = $admin_u_id;
	$temp['admin_log_action'] = $action;
	$temp['admin_log_time'] = time();
	$temp['admin_log_type'] = $log_type;
	$temp['ip_address'] = getUserIP();
	$temp['admin_log_inof'] = $log_info;
	if(defined('ADMIN_USE_LOG4J') && ADMIN_USE_LOG4J)
	{
		include_once(LOG4J_PATH."Logger.php");
		Logger::configure(ADMIN_LOG4J_CONFIG);
		$logger = Logger::getLogger('admin');
		unset($temp['admin_log_id']);
		//$temp['admin_log_inof'] = serialize($temp['admin_log_inof']);
		$log_msg = implode(' ',$temp);
		$logger->debug($log_msg);
	}else{
		$db = Factory::getDB('admin_log');
		$db->insert_admin_log($temp);
		//$error_sql = end($db->getDebugMsg());
		//echo $error_sql;
	}
}

//$permission_arr,$admin_permission_arr,$post_name = "admin_group_permission";
function smarty_create_popedom($params)
{
	extract($params);
	$temp = "";
	foreach ($permission_arr AS $k=>$v)
	{
		$temp.="<strong>{$v['title']}</strong><br>";
		foreach ($v['method'] as $key=>$value)
		{
				$temp.="&nbsp;&nbsp;&nbsp;&nbsp;";
				$temp.="<input type=checkbox name='{$post_name}[{$k}][]' value={$value[1]}";
				$temp.=(isset($admin_permission_arr[$k])&&parse_power($admin_permission_arr[$k],$value[1])) ? " checked " : "";
				$temp.=">{$value[0]}";
		}
		$temp.="<br>";
	}
	return $temp;
}

?>